using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using OgilvyOne.DALFactory;
using OgilvyOne.IDAL;
using OgilvyOne.Model;


namespace OgilvyOne.BLL
{
    public class User
    {
        // Methods
        public bool CheckIsPriv(string sloginname, string sPwd, string sMenuUid)
        {
            bool flag = false;
            try
            {
                int userid = this.getUserUID(sloginname, sPwd);
                string sroleid = new RoleUser().getRoleByUserID(userid);
                string funcid = new FuncRole().getFuncByRoleUID(sroleid);
                string str3 = new MenuFunc().getMenuByFuncUID(funcid);
                HttpContext.Current.Session["MENU_KEY"] = str3;
                flag = true;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return flag;
        }

        public UserInfo CheckLogon(string login_name, string password)
        {
            if ((login_name.Trim() == string.Empty) || (password.Trim() == string.Empty))
            {
                return null;
            }
            UserInfo info = OgilvyOne.DALFactory.User.Create().getUserInfo(login_name, password);
            this.CheckIsPriv(login_name, password, "");
            return info;
        }

        public bool CheckLogon(string sloginname, string sPwd, string sMenuUid)
        {
            bool flag = false;
            try
            {
                int userid = this.getUserUID(sloginname, sPwd);
                string sroleid = new RoleUser().getRoleByUserID(userid);
                string funcid = new FuncRole().getFuncByRoleUID(sroleid);
                MenuFunc func = new MenuFunc();
                string[] strArray = func.getMenuByFuncUID(funcid).Split(new char[] { ',' });
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (strArray[i].Equals(sMenuUid))
                    {
                        flag = true;
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return flag;
        }

        public void Delete(int user_uid)
        {
            OgilvyOne.DALFactory.User.Create().Delete(user_uid);
        }

        public IList getUserList()
        {
            return OgilvyOne.DALFactory.User.Create().getUserList();
        }

        public IList getUserList(int userid)
        {
            return OgilvyOne.DALFactory.User.Create().getUserList(userid);
        }

        public IList getUserList(string status)
        {
            return OgilvyOne.DALFactory.User.Create().getUserList(status);
        }

        public IList getUserList(string user_name, string user_logon_code)
        {
            return OgilvyOne.DALFactory.User.Create().getUserList(user_name, user_logon_code);
        }

        public IList getUserListByCode(string user_logon_code)
        {
            return OgilvyOne.DALFactory.User.Create().getUserListByCode(user_logon_code);
        }

        public IList getUserListByName(string user_name)
        {
            return OgilvyOne.DALFactory.User.Create().getUserListByName(user_name);
        }

        public int getUserUID(string user_name, string user_password)
        {
            return OgilvyOne.DALFactory.User.Create().getUserID(user_name, user_password);
        }

        public void Insert(UserInfo user)
        {
            if (user.sUser_Logon_Code.Trim() != string.Empty)
            {
                OgilvyOne.DALFactory.User.Create().Insert(user);
            }
        }

        public void Update(UserInfo user)
        {
            if (user.sUser_Logon_Code.Trim() != string.Empty)
            {
                OgilvyOne.DALFactory.User.Create().Update(user);
            }
        }

    }
}
